from docx import Document
from docx.shared import Pt, Inches  # 磅数，图片格式
from docx.oxml.ns import qn  # 中文格式
from docx.enum.text import WD_ALIGN_PARAGRAPH

li1 = ['纯文字1', '纯文字2', '纯文字3']
# 写入纯文字
for i in li1:
    doc = Document()
    # 设置文档的基础字体
    doc.styles['Normal'].font.name = u'宋体'
    doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'),u'宋体')

    # 建立一个自然段
    p1 = doc.add_paragraph()
    # 居中对齐，默认为左对齐
    p1.alignment = WD_ALIGN_PARAGRAPH.CENTER
    # 设置字体
    run1 = p1.add_run('标题')
    run1.font.name = '微软雅黑'
    run1._element.rPr.rFonts.set(qn('w:eastAsia'), u'微软雅黑')
    run1.font.size = Pt(21)
    # 加粗
    run1.font.bold = True
    # 右边距5磅
    p1.space_after = Pt(5)
    # 左边距5磅
    p1.space_before = Pt(5)

    p2 = doc.add_paragraph()
    p2.alignment = WD_ALIGN_PARAGRAPH.CENTER
    run2 = p2.add_run('新标题')
    run2.font.name = '楷体'
    run2._element.rPr.rFonts.set(qn('w:eastAsia'), u'楷体')
    run2.font.size = Pt(18)
    p2.space_after = Pt(5)
    p2.space_before = Pt(5)

    p3 = doc.add_paragraph()
    run3 = p3.add_run('地球的形状和大小：')
    run3.font.name = '黑体'
    run3._element.rPr.rFonts.set(qn('w:eastAsia'), u'黑体')
    run3.font.size = Pt(15)
    p3.space_after = Pt(5)
    p3.space_before = Pt(5)

    p4 = doc.add_paragraph()
    run4 = p4.add_run('    地球是一个两极稍扁、赤道略鼓的球体。它的赤道半径是6378千米，两极方向的半径比赤道半径短21千米左右，仅差0.33%，它的赤道周长约为4万千米。')
    run4.font.name = '黑体'
    run4._element.rPr.rFonts.set(qn('w:eastAsia'), u'黑体')
    run4.font.size = Pt(10)
    p4.space_after = Pt(5)
    p4.space_before = Pt(5)

    doc.save(f'test{i}.docx')

li2 = ['图和表1','图和表2']
for i in li2:
    doc = Document()
    doc.styles['Normal'].font.name = u'楷体'
    doc.styles['Normal'].font.size = Pt(14)
    doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'楷体')
    # 插图
    doc.add_picture('苦短.jpg', width=Inches(5))

    p1 = doc.add_paragraph()
    p1.alignment = WD_ALIGN_PARAGRAPH.CENTER
    run1 = p1.add_run('    地球是一个两极稍扁、赤道略鼓的球体。它的赤道半径是6378千米，两极方向的半径比赤道半径短21千米左右，仅差0.33%，它的赤道周长约为4万千米。')
    run1.font.name = '微软雅黑'
    run1._element.rPr.rFonts.set(qn('w:eastAsia'), u'微软雅黑')
    run1.font.size = Pt(10)
    p1.space_after = Pt(5)
    p1.space_before = Pt(5)

    # 插表格
    table = doc.add_table(rows=3,cols=3,style='Table Grid')
    # 单元格合并
    table.cell(0,0).merge(table.cell(0,2))
    # 居中
    table.cell(0, 0).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
    # 表格字体
    table_run1 = table.cell(0,0).paragraphs[0].add_run('xx产品报价表')
    table_run1.font.name = u'隶书'
    table_run1._element.rPr.rFonts.set(qn('w:eastAsia'), u'隶书')

    table.cell(1,0).text = '日期'
    table.cell(1,1).text = '价格'
    table.cell(1,2).text = '备注'
    table.cell(2,0).text = '2019.11.1'
    table.cell(2,1).text = '111'
    table.cell(2,2).text = ''

    p2 = doc.add_paragraph()
    run2 = p2.add_run('（联系人：xxx     电话：12345678）')
    run2.font.name = '黑体'
    run2._element.rPr.rFonts.set(qn('w:eastAsia'), u'黑体')
    run2.font.size = Pt(16)
    run2.font.bold = True

    # 翻到新的一页
    doc.add_page_break()
    # 再插一段
    p3 = doc.add_paragraph()
    run3 = p3.add_run('广告zzzzzzzzzzzzzzz')
    doc.save(f'test{i}.docx')